<?php
if(!defined('BASEPATH')) exit('No direct script access allowed');

// +----------------------------------------------------------------------+
// | Authors	: Tung, Nguyen Dam                                        |
// | Email		: mask_final@yahoo.com									  | 								
// | Mobile		: (+84) 936 885 466                                       |
// | Date		: 06/2011												  |							
// +----------------------------------------------------------------------+
// | Copyrights (C) 2011                                                  |
// | All rights reserved                                                  |
// +----------------------------------------------------------------------+

if(!function_exists('send_mail')){
    function send_mail($e_to, $e_from, $e_subject, $template, $arrKeyVal = array()){
		// assign variable for template
		foreach($arrKeyVal as $key => $val){
			$var = $key;
			$$var = $val;		
		}
		
		// Get content file template and excute
		$file = file_get_contents($template);
		
		$pattern = '/<#(.*)#\>/';
		preg_match_all($pattern, $file, $match);
		
		$arrKey = array();
		$arrVal = array();
		
		foreach($match[0] as $key => $val){
			array_push($arrKey, $val);
			array_push($arrVal, $$match[1][$key]);
		}
		
		$message = str_replace($arrKey, $arrVal, $file);
		
		
		// Filter
		$host 		= 'http://' . $_SERVER['HTTP_HOST'] . '/'; 
		$arrFilter 	= array('url(', 'src="', 'href="*');
		$arrReplace	= array('url(' . $host, 'src="' . $host, 'href="' . $host);
		$message = str_replace($arrFilter, $arrReplace, $message);
			
		$headers = "MIME-Version: 1.0" . "\r\n";					
		$headers .= "Content-type:text/html; charset=utf-8" . "\r\n";
		$headers .= "X-Mailer: PHP/" . phpversion() . "\r\n";
		$headers .= "From: $e_from " . "\r\n";				
		$headers .= "Reply-to: $e_from";
		mail($e_to, $e_subject, $message, $headers);
	}   
}
